﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CNotebook;

namespace NotebookS2
{
    public partial class FND_POLICY : Form
    {
        public FND_POLICY()
        {
            InitializeComponent();
        }

        public string ConnectionString { get; set; }
        public string SelectedGroupNum { get; set; }
        public string SelectedPolicyName { get; set; }

        void BindPolicyByGroupNumber()
        {
            string _SEARCH_TEXT = txtSearch.Text;

            PolicyManager oManager = new PolicyManager();
            PolicyUnit oUnit = new PolicyUnit();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            DataTable dtPolicies = oManager.BrowsePolicyByGroupNumber(_SEARCH_TEXT);

            oManager.Close();

            gridControl1.DataSource = dtPolicies;
            gridControl1.Refresh();

        }

        void BindPolicyByPolicyName()
        {
            string _SEARCH_TEXT = txtSearch.Text;

            PolicyManager oManager = new PolicyManager();
            PolicyUnit oUnit = new PolicyUnit();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            DataTable dtPolicies = oManager.BrowsePolicyByPolicyName(_SEARCH_TEXT);

            oManager.Close();

            gridControl1.DataSource = dtPolicies;
            gridControl1.Refresh();
        }

        private void FND_POLICY_Load(object sender, EventArgs e)
        {
            cboSearchBy.SelectedIndex = 1;
        }

        private void txtSearch_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (cboSearchBy.SelectedIndex == 0)
            {
                BindPolicyByGroupNumber();
            }

            if (cboSearchBy.SelectedIndex == 1)
            {
                BindPolicyByPolicyName();
            }
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            this.SelectedGroupNum = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["GroupCode"]).ToString();
            this.SelectedPolicyName = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["PolicyGroupName"]).ToString();

            this.Close();
        }

        private void btnChoose_Click(object sender, EventArgs e)
        {
            this.SelectedGroupNum = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["GroupCode"]).ToString();
            this.SelectedPolicyName = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["PolicyGroupName"]).ToString();

            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.SelectedGroupNum = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["GroupCode"]).ToString();
                this.SelectedPolicyName = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["PolicyGroupName"]).ToString();

                this.Close();
            }
        }
    }
}
